package com.jjwxc.jwjskandriod.readActivity.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.view.PageLoader;
import com.jjwxc.jwjskandriod.readActivity.view.PageMode;
import com.jjwxc.jwjskandriod.readActivity.view.PageStyle;

public class ReadTurningDialog extends Dialog {
    private PageLoader mPageLoader;
    private Activity mActivity;

    private int mBrightness;
    private int mTextSize;

    private boolean isTextDefault;
    private PageMode mPageMode;
    private PageStyle mPageStyle;
    LinearLayout ll_fanhui;
    LinearLayout ll_read_turn;
    RadioGroup mRgPageMode;

    RadioButton mRbSimulation;
    RadioButton mRbCover;
    RadioButton mRbSlide;

    private ReadSettingManager mSettingManager;

    public ReadTurningDialog(@NonNull Activity activity, PageLoader mPageLoader) {
        super(activity);
        mActivity = activity;
        this.mPageLoader = mPageLoader;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_read_turning);

        setUpWindow();
        initData();
        initWidget();
        initClick();
    }


    //设置Dialog显示的位置
    private void setUpWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        window.getDecorView().setBackground(null);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    private void initData() {
        mSettingManager = ReadSettingManager.getInstance();

        mBrightness = mSettingManager.getBrightness();
        mTextSize = mSettingManager.getTextSize();
        isTextDefault = mSettingManager.isDefaultTextSize();
        mPageMode = mSettingManager.getPageMode();
        mPageStyle = mSettingManager.getPageStyle();
    }

    private void initWidget() {
        mRgPageMode = findViewById(R.id.read_setting_rg_page_mode);
        mRbSimulation = findViewById(R.id.read_setting_rb_simulation);
        mRbCover = findViewById(R.id.read_setting_rb_cover);
        mRbSlide = findViewById(R.id.read_setting_rb_scroll);
        ll_fanhui = findViewById(R.id.ll_fanhui);
        ll_read_turn = findViewById(R.id.ll_read_turn);

        //暗黑模式
        if(mSettingManager.isNightMode()){
            ll_read_turn.setBackgroundResource(R.drawable.shape_black);
        }else{
            ll_read_turn.setBackgroundResource(R.drawable.shape_while);

        }

        initPageMode();
    }

    private void initPageMode() {
        switch (mPageMode) {
            case SIMULATION:
                mRbSimulation.setChecked(true);
                break;
            case COVER:
                mRbCover.setChecked(true);
                break;
            case SCROLL:
                mRbSlide.setChecked(true);
                break;
//            case NONE:
//                mRbNone.setChecked(true);
//                break;
//            case SCROLL:
//                mRbScroll.setChecked(true);
//                break;
//            case LEFT_HAND:
//                readSettingRbLeft.setChecked(true);
//                break;
        }
    }

    @SuppressLint("NonConstantResourceId")
    private void initClick() {
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //Page Mode 切换
        mRgPageMode.setOnCheckedChangeListener(
                (group, checkedId) -> {
                    PageMode pageMode;
                    switch (checkedId) {
                        case R.id.read_setting_rb_cover:
                            pageMode = PageMode.COVER;
                            break;
                        case R.id.read_setting_rb_scroll:
                            pageMode = PageMode.SCROLL;
                            break;
//                        case R.id.read_setting_rb_none:
//                            pageMode = PageMode.NONE;
//                            break;
//                        case R.id.read_setting_rb_left:
//                            pageMode = PageMode.LEFT_HAND;
//                            break;
                        default:
                            pageMode = PageMode.SIMULATION;
                            break;
                    }
                    mPageLoader.setPageMode(pageMode);
                }
        );
    }
}
